package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.web.admin.mapper.LeaseTermMapper;
import com.atguigu.lease.web.admin.service.LeaseTermService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.LeaseTerm;
import org.springframework.stereotype.Service;

/**
* @author 1
* @description 针对表【lease_term(租期)】的数据库操作Service实现
* @createDate 2024-08-28 11:00:26
*/
@Service
public class LeaseTermServiceImpl extends ServiceImpl<LeaseTermMapper, LeaseTerm>
    implements LeaseTermService {

    @Override
    public Boolean saveOrUpdateTerm(LeaseTerm term) {
        //对term的值进行校验：   租期和单位 组合唯一
        long id = term.getId()==null?-1L:term.getId();//获取更新的id 和 新增一定不存在的id
        LeaseTerm leaseTerm = this.getOne(Wrappers.lambdaQuery(LeaseTerm.class)
                .eq(LeaseTerm::getMonthCount, term.getMonthCount())
                .eq(LeaseTerm::getUnit, term.getUnit())
                .last("limit 1"));

        if(leaseTerm!=null && !leaseTerm.getId().equals(id)){
            throw new StgyException(ResultCodeEnum.TERM_EXISTS_ERROR);
        }
        return this.saveOrUpdate(term);
    }
}




